package sql.parser;

import sql.lexer.SQLLexer;

public class DeleteParser extends SelectParser {
    public DeleteParser(SQLLexer lexer) {
        super(lexer);
    }

    public SyntaxNode deleteParse() {
        this.nextToken();
        SyntaxNode sqlNode = new SyntaxNode(SyntaxNodeType.Sql);
        if(deleteSql(sqlNode) && this.isEnd()) {
            return sqlNode;
        } else {
            return null;
        }
    }

    private boolean deleteSql(SyntaxNode sqlNode) {
        if(this.getSyntaxType() == SyntaxType.Delete) {
            nextToken();
            //from关键字可以没有
            if(this.getSyntaxType() == SyntaxType.From) {
                nextToken();
            }
            if(this.getSyntaxType() == SyntaxType.Identifier) {
                SyntaxNode fromNode = new SyntaxNode(SyntaxNodeType.From, this.getStrToken());
                sqlNode.setFromNode(fromNode);
                nextToken();
                if(this.isEnd()) {
                    return true;
                } else {
                    if(this.getSyntaxType() == SyntaxType.Where) {
                        nextToken();
                        if(!conditions(sqlNode)) {
                            throw parseException();
                        }  else {
                            return true;
                        }
                    }
                }
            }
        }
        throw parseException();
    }
}
